﻿using System;
using AppToolKit.Controller.Macro;
using AppToolKit.Data;
using Common.Logging;
using AppToolKit.Core.Context.Spring;
using System.Data;
using PMSBusiness.Core.Logger;
using System.Xml;
using PMSBusiness.Core.Toolkit;

namespace PMSBusiness.Service.Background
{
    /// <summary>
    /// 数据库任务
    /// </summary>
    [AutoWiring]
    public class DbTask : WorkTaskBase
    {
        private string key;
        private string value;
        private MacroContext context_;
        private TaskMacro taskMacro_;
        [AutoWiring]
        public IDbOperation DbOperation { get; set; }
        
        private static ILog logger_ = LogManager.GetLogger(LoggerFixure.LoggerConfigure.DefaultLogger);

        public DbTask(string strKey,string strValue)
        {
            key = strKey;
            value = strValue;
        }
        public DbTask()
        {
        }
        public override void Execute()
        {
           

            context_ = new MacroContext("${", "}");
            taskMacro_ = new TaskMacro();
            taskMacro_.RegisterMacro(context_);
            string strSQL = context_.ApplyMacro(value);
            int intResult;
            try
            {
                intResult = DbOperation.ExecuteNonQuery(CommandType.Text,strSQL);
                logger_.Debug(string.Format("执行了过程:{0},影响{1}", strSQL, intResult));
                
            }
            catch (Exception ex)
            {
                logger_.Debug(string.Format("执行了过程:{0}。产生了异常。{1}", strSQL, ex.Message));
                throw;
            }
            
        }

        public override void ReadConfigure(XmlNode node)
        {
            key = XmlReadWriteHelper.GetNodeValue(node, "key");
            value = XmlReadWriteHelper.GetNodeValue(node, "value");

            //throw new NotImplementedException();  
        }
    }
}
